<template>
  <el-dialog
    :title="title"
    :visible="addlogVisible"
    width="580px"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <div class="form-container">
      <el-form ref="From" :model="addForm" :rules="rules">
        <el-form-item label="一体杆名称" prop="poleName">
          <el-input v-model="addForm.poleName" />
        </el-form-item>
        <el-form-item label="一体杆编号" prop="poleNumber">
          <el-input v-model="addForm.poleNumber" />
        </el-form-item>
        <el-form-item label="一体杆IP" prop="poleIp">
          <el-input v-model="addForm.poleIp" />
        </el-form-item>
        <el-form-item label="关联区域" prop="areaId">
          <el-select v-model="addForm.areaId" placeholder="请选择">
            <el-option
              v-for="item in areaList"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="一体杆类型" prop="poleType">
          <el-select v-model="addForm.poleType" placeholder="请选择">
            <el-option label="入口" value="entrance" />
            <el-option label="出口" value="export" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getAreaListAPI, addoneAPI, getListAPI, editListAPI } from '@/api/one'
export default {
  name: 'Rodmanage',
  props: {
    addlogVisible: {
      type: Boolean
    }
  },
  data() {
    return {
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      },
      areaList: [],
      tatusList: {
      },
      rules: {
        poleName: [
          { required: true, message: '请输入一体杆名称', trigger: 'blur' },
          { min: 6, mix: 20, message: '一体杆名称长度要在6至20之间', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '一体杆编号', trigger: 'blur' },
          { min: 6, mix: 20, message: '一体杆编号长度要在6至20之间', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入一体杆IP', trigger: 'blur' },
          { pattern: /^([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/, message: '正确格式的ip', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      if (this.addForm.id) {
        return '编辑一体杆'
      } else {
        return '添加一体杆'
      }
    }
  },
  created() {
    this.getAreaList()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
      this.$refs.From.resetFields()
    },
    // 渲染区域下拉框
    async getAreaList() {
      const res = await getAreaListAPI()
      this.areaList = res.data
    },
    // 确定按钮的分支结构
    confirm() {
      this.$refs.From.validate(async flag => {
        if (this.addForm.id) {
          await editListAPI(this.addForm)
          this.$message.success('编辑成功')
        } else {
          await addoneAPI(this.addForm)
          this.$message.success('添加成功')
        }
        this.$emit('randerList')
        this.closeDialog()
      })
    },
    // 数据回填
    async editCommand(value) {
      const res = await getListAPI({ page: 1, pageSize: 10, poleName: value })
      const { poleName, poleNumber, poleIp, areaId, poleType, id } = res.data.rows[0]
      this.addForm = { poleName, poleNumber, poleIp, areaId, poleType, id }
    }
  }
}
</script>

<style>

</style>
